﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using User.Api.Common;
using User.Api.Models;
using AutoMapper;

namespace User.Api.Controllers
{
    /// <summary>
    /// 用户来源接口
    /// </summary>
    public class UserSourceController : BaseApiController
    {
        /// <summary>
        /// 添加接口
        /// </summary>
        /// <param name="reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<ResponseCommon<int>> Add(RequestUserSourceAdd reqInfo)
        {
            var result = ResultData<ResponseCommon<int>>.Error("");
            if (reqInfo.SourceToken.IsEmpty())
            {
                throw new BizException(StatusCodeEnum.UserTokenNotNull);
            }
            var oExist = UserSourceOperate.GetByToken(reqInfo.SourceToken, reqInfo.SourceType);
            if (oExist != null)
            {
                throw new BizException(StatusCodeEnum.UserSourceExists);
            }
            var oInfo = Mapper.Map<UserSourceInfo>(reqInfo);
            oInfo.IsActive = 1;
            oInfo.CreatedDate = DateTime.Now;
            var userSourceID = UserSourceOperate.Insert(oInfo);
            result = ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(userSourceID));
            return result;
        }

        /// <summary>
        /// 用户来源信息获取
        /// </summary>
        /// <param name="UserSourceID">用户来源ID</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        [HttpGet]
        public ResultData<ResponseUserSourceGet> Get(int UserSourceID)
        {
            var result = ResultData<ResponseUserSourceGet>.Error("");
            if (UserSourceID <= 0)
            {
                throw new BizException(StatusCodeEnum.UserSourceIDNotNull);
            }
            var oInfo = UserSourceOperate.GetByID(UserSourceID);
            if (oInfo != null)
            {
                var oRes = Mapper.Map<ResponseUserSourceGet>(oInfo);
                result = ResultData<ResponseUserSourceGet>.Success(oRes);
            }
            else
            {
                throw new BizException(StatusCodeEnum.NoExistInfo);
            }
            return result;
        }

        /// <summary>
        /// 更新用户头像
        /// </summary>
        /// <param name="reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<int> UpdateHead(ReqUserHeadUpdate reqInfo)
        {
            //更新用户来源偶像
            int result = UserSourceOperate.UpdateUserHead(reqInfo.SourceUserHead, reqInfo.SourceUserName,reqInfo.SourceToken, reqInfo.SourceType);
            UserSourceInfo userSourceInfo = UserSourceOperate.GetByToken(reqInfo.SourceToken, reqInfo.SourceType);
            if (userSourceInfo != null && userSourceInfo.UserID>0)
            {
                //更新用户偶像
                UserOperate.UpdateUserHead(userSourceInfo.UserID, userSourceInfo.SourceUserName, userSourceInfo.SourceUserHead);
            }
            return ResultData<int>.Success(result);
        }

        /// <summary>
        /// 根据唯一标识和来源媒体获取信息
        /// </summary>
        /// <param name="sourceToken">唯一标识符</param>
        /// <param name="sourceType">来源媒体</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<ResponseUserSourceGet> GetByToken(string sourceToken, int sourceType)
        {
            var result = ResultData<ResponseUserSourceGet>.Error("");
            var oInfo = UserSourceOperate.GetByToken(sourceToken, sourceType);
            if (oInfo != null)
            {
                var oRes = Mapper.Map<ResponseUserSourceGet>(oInfo);
                result = ResultData<ResponseUserSourceGet>.Success(oRes);
            }
            else
            {
                throw new BizException(StatusCodeEnum.NoExistInfo);
            }
            return result;
        }
    }
}